package service

import (
	"context"
	"github.com/sirupsen/logrus"
	v1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
)

func CreateService(clientset *kubernetes.Clientset, service *v1.Service, namespace string, log *logrus.Logger) (*v1.Service, error) {
	log.Infof("Creating Service...")
	createdService, err := clientset.CoreV1().Services(namespace).Create(context.TODO(), service, metav1.CreateOptions{})
	if err != nil {
		log.Errorf("Failed to create Service: %v", err)
		return nil, err
	}
	log.Infof("Created Service: %s", createdService.Name)
	return createdService, nil
}

func DeleteService(clientset *kubernetes.Clientset, service *v1.Service, namespace string, log *logrus.Logger) error {
	log.Infof("Deleting Service...")
	err := clientset.CoreV1().Services(namespace).Delete(context.TODO(), service.Name, metav1.DeleteOptions{})
	if err != nil {
		log.Errorf("Failed to delete Service: %v", err)
		return err
	}
	log.Info("Service deleted")
	return nil
}
